<!DOCTYPE html>
<html>

<head>
    <title></title>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="../../Css/bootstrap.css" />
    <link rel="stylesheet" type="text/css" href="../../Css/bootstrap-responsive.css" />
    <link rel="stylesheet" type="text/css" href="../../Css/style.css" />
    <script type="text/javascript" src="../../Js/jquery.js"></script>
    <script type="text/javascript" src="../../Js/bootstrap.js"></script>
    <script type="text/javascript" src="../../Js/ckform.js"></script>
    <script type="text/javascript" src="../../Js/common.js"></script>

    <style type="text/css">
        body {
            padding-bottom: 40px;
        }
        .sidebar-nav {
            padding: 9px 0;
        }

        @media (max-width: 980px) {
            /* Enable use of floated navbar text */
            .navbar-text.pull-right {
                float: none;
                padding-left: 5px;
                padding-right: 5px;
            }
        }


    </style>

</head>

<body>
<form id="myForm" action="index.html" method="post" class="definewidth m20">
    <!--<input type="hidden" name="m_id" value="{$user.id}" />-->
    <table class="table table-bordered table-hover definewidth m10">
        <tr>
            <td width="10%" class="tableleft">资源名称</td>
            <td><input type="text" name="menuName" value=""/></td>
        </tr>
        <tr>
            <td class="tableleft">url</td>
            <td><input type="text" name="url" value=""/></td>
        </tr>
        <tr>
            <td class="tableleft">是否有效</td>
            <td>
                <input type="radio" name="status" value="0" checked/> 有效
                <input type="radio" name="status" value="1" /> 无效
            </td>
        </tr>
        <tr>
            <td class="tableleft"></td>
            <td>
                <button class="btn btn-primary" type="button" id="btnUpdate">更新</button>&nbsp;&nbsp;
                <button type="button" class="btn btn-success" name="backid" id="backid">返回列表</button>
            </td>
        </tr>
    </table>
</form>
</body>
</html>
<script>
    String.prototype.GetValue= function(para) {
        let reg = new RegExp("(^|&)"+ para +"=([^&]*)(&|$)");
        let r = this.substr(this.indexOf("\?")+1).match(reg);
        if (r!=null) return unescape(r[2]); return null;
    }
    $(function () {
        let url = document.location.toString();//获取URL
        let menuId=url.GetValue("menuId");
        let pageNum=url.GetValue("pageNum");
        let pageSize=url.GetValue("pageSize");

        //根据id查询要更新的菜单信息并回显到页面
        queryById(menuId);

        //提交按钮的单击事件--实现真正的更新
        $("#btnUpdate").click(function () {
            $.ajax({
                type: "POST",//这里必须写POST
                url: "/menu/"+menuId,
                data: $("#myForm").serialize()+"&_method=PUT",
                dataType:"json",
                success: function(vo){
                    if(vo.code==200) {
                        window.location.href = "index.html?pageNum="+pageNum+"&pageSize="+pageSize;
                    }else{
                        alert("更新失败！"+vo.msg);
                    }
                }
            });
        });

        $('#backid').click(function(){
            window.location.href = "index.html?pageNum="+pageNum+"&pageSize="+pageSize;
        });
    });

    //回显
    function queryById(menuId) {
        $.ajax({
            type: "GET",
            url: "/menu/"+menuId, //RESTful风格的API定义
            data: "",
            success: function (vo) {
                let obj=vo.obj;
                console.log("待更新的资源:"+obj.toString());
                let id=obj.id;
                let menuName=obj.menuName==null?'':obj.menuName;
                let url=obj.url==null?'':obj.url;
                let status=obj.status==null?'':obj.status;;
                $("input[name='menuName']").val(menuName);
                $("input[name='url']").val(url);
                $("input[name='status'][value='" + status + "']").prop("checked", "checked");
            }
        });
    }
</script>